﻿using MediatR;
using W.Warehouse.Domain.AllocationProduct;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Read.Api.Application.Command.AllocationCommand;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Read.Api.Application.CommandHandlder.AllocationCommandHandler
{
    public class AllocationProductModelCommandHanlder:IRequestHandler<AllocationProductModelCommand, AllocationModelDTO>
    {
        private readonly IAllocationProductRepository _iallocationProductRepository;
        private readonly IAllocationRepository _iallocationRepository;
        public AllocationProductModelCommandHanlder(IAllocationProductRepository iallocationProductRepository, IAllocationRepository iallocationRepository)
        {
            _iallocationProductRepository = iallocationProductRepository;
            _iallocationRepository = iallocationRepository;
        }

        public async Task<AllocationModelDTO> Handle(AllocationProductModelCommand request, CancellationToken cancellationToken)
        {
            var products = await _iallocationProductRepository.GetAllAsync();
            var allocation = from a in await _iallocationRepository.GetAllAsync()
                          where a.IsDelete == false && a.AllocationId == request.AllocationId
                          select new AllocationModelDTO
                          {
                              AllocationId = a.AllocationId,
                              AllocationNo = a.AllocationNo,
                              WarehouseId = a.WarehouseId,
                              AWarehouseName = a.AWarehouseName,
                              AssociateNumber = a.AssociateNumber,
                              SingleMaker = a.SingleMaker,
                              SingleTime = a.SingleTime,
                              AllocationRemarks = a.AllocationRemarks,
                              AllocationStatus = a.AllocationStatus,
                              AllocationStatusText=a.AllocationStatus.ToString(),
                              ExamineReason = a.ExamineReason,
                              ExaminePeople = a.ExaminePeople,
                              ExamineTime = a.ExamineTime,
                              officeId = a.officeId,
                              userid = a.userid,
                              allocationProductList = products.Where(x => x.AllocationId == a.AllocationId).Select(x => new AllocationProductList
                              {
                                  AllocationProductId = x.AllocationProductId,
                                  ProductName = x.ProductName,
                                  ProductNo = x.ProductNo,
                                  ProductSpec = x.ProductSpec,
                                  ProductBatch = x.ProductBatch,
                                  ProductPrice = x.ProductPrice,
                                  Quantity = x.Quantity,
                                  BinLocationName = x.BinLocationName,
                                  WarehouseId = x.WarehouseId,
                                  officeId = x.officeId,
                                  userid = x.userid,
                                  AllocationId = x.AllocationId,
                              }).ToList()
                          };
            return allocation.FirstOrDefault();
        }
    }
}
